import json
import requests
from urllib.parse import quote

from src.appointment_utils import get_available_appointment_dates, get_available_slots_by_date
from src.email_helper import EmailHelper
from src.user import User
from src import utils


def create_appointment(cookies):
    locationId = '8d780684-1524-4bda-b138-7c71a8591944'
    data = {
        'AppointmentDate1': '2024-04-05',
        'AppointmentSlot1': quote('08:00-08:30'),
        'ApplicantsNo': '1',
        'MissionId': '',
        'LocationId': locationId,
        "VisaType': '3033c6d3-579b-47e1-9602-91368d63025c"
        "VisaSubTypeId': 'd6f2b878-181f-4584-a595-babdbcac4298"
        "AppointmentCategoryId': '5c2e8e01-796d-4347-95ae-0c95a9177b26"
        'Mobile': '18519203764',
        'MobileCountryCode': '%2B86',
        'Id': '56f41f5c-f58d-4989-9079-45436a89628e',
        'Email': '1248198090%40qq.com',
        "EmailVerificationCode': '374166"
        "MobileVerificationCode': '"
        'EmailCode': 'tRyE1AJ1rZf5pvz1iDUn6UU19hH4Yw8JfwqAgca%2F65We8weOnoHkrqfbKoUD1jnTVTUevyiVxkj8fSHcfKjyu93wlLp35MLYuz06X4EKip4KLFFu1XkbfnE0v3WMqXn%2B',
        'MobileCode': 'Lz0ziRpsxsyDa6ahBOqYrUQ02KlifbLZGFSlKsYUF8SGPAPiVFac9WCp%2FgDzHJzIFRSVQ3uVgdwOs7NRJRZVZhVpSCXaDMQazIOjDrWh7azvq%2FWg9fsb3NMDEsSKHBwb',
        'ScriptData': 'jo%2BWrPiNo78ZDGuNpLOXpFWMHq7kxdplTj87DDhgVC8gKguGItmoMQbqezrNm3Frqwk3%2B%2FiHHv%2FqnYTOJjpcJjDSaWv33WkEXVinXlIziKM%3D',
        'AppointmentId': '56f41f5c-f58d-4989-9079-45436a89628e',
        'ImageId': '',
        'DataAction': 'Create',
        'nextSectionId': '',
        'ApplicantPhotoId': '3005f2e1-d2a4-48a8-a46b-c1a7d2f526c1',
        'AppointmentDetailsList': '%5B%5D',
        'FullSlot': '',
        'Holidays': '',
        'WeekDays': '',
        'MaximumAllowedDays': '0',
        'SaveState': 'Appointment',
        'CaptchaId': 'b015dda5-363f-4284-8ebc-f125b6a8ba66',
        'CaptchaData': 'w8EdsIIBWV%2BXapRD8VDRTbdnwcHc2tMIzHFbwB6KS%2BaPRR0yTkEvhDVl36YVWGYYqNdGy03USV4fEg3JhL0HK1TCd2HPG5HnxIZ7dWgCJSDdUu3qEoMJGTRkKHWXIDS88ml1fR%2BwOqjtT6kidgGAOkR%2Bdj8fJniYRFBRZYKNz5p9iG4wZyZGzVoxU6dWQwFENvSUZtEdCuLP0sn3o%2Fytjg3KH67nTZcSmLWaZ0YPKBayADPgc9CLSB0iMphpfOdAvoRXAn7O9h%2Fp5x1lfqt%2BwQ%3D%3D',
        'AppointmentNo': '',
        'EmailVerified': 'True',
        'MobileVerified': 'False',
        'DataSource': 'WEB_BLS',
        'VisaType': '3033c6d3-579b-47e1-9602-91368d63025c',
        'AppointmentFor': 'Individual',
        'ServerAppointmentDate': '2024-04-05',
        '__RequestVerificationToken': 'CfDJ8ItFsDhqkWRIiWYGIDDs_SNgxmgfhD56DbAIkFZ2x-G_BLAWvaAWUiYm75m8DKc99fNhW62UTirqnjLTmnY8lDUqmx7f02mRIdVQ38X82gBf_5c6ZRnqE6l3cQfyfDLjet8gqWw1lEhI80wZKU9yeM-2l9iXHuV5EXK5CLm31d8ZYQAOy0V6sgX3j_olXLSJfg',
        'X-Requested-With': 'XMLHttpRequest',
    }
    url = 'https://spain.blscn.cn/CHN/BLSAppointment/ManageAppointment'
    headers = utils.get_headers()
    res = requests.post(url, headers=headers, cookies=cookies, data=data)
    print(res.status_code)


def edit_appointment(cookies):
    applicantsDetailsList = [
        {
            "ApplicantSerialNo": "1", "FirstName": "zheng", "SurName": "zheng", "LastName": "changjun",
            "ServerDateOfBirth": "1990-03-25", "PlaceOfBirth": "北京",
            "CountryOfBirthId": "5e44cd63-68f0-41f2-b708-0eb3bf9f4a72",
            "NationalityId": "5e44cd63-68f0-41f2-b708-0eb3bf9f4a72",
            "NationalityAtBirthId": "5e44cd63-68f0-41f2-b708-0eb3bf9f4a72",
            "GenderId": "2a299467-0281-4efc-bd69-0cba402d6b26",
            "MaritalStatusId": "23c3d9ec-f504-4109-89b5-804189ecb4c1",
            "IsMinor": False, "MinorParentSurname": "", "MinorParentFirstName": "", "MinorParentLastName": "",
            "MinorParentNationalityId": "", "MinorParentAddress": "", "NationalIdentityNumber": "",
            "PassportType": "ea3865a7-edae-4768-8883-3bc9339bdd85", "PassportNo": "2300000000",
            "ServerPassportIssueDate": "2024-03-18", "ServerPassportExpiryDate": "2025-07-05",
            "ServerTravelDate": "2024-03-30", "IssuePlace": "121212121212",
            "IssueCountryId": "5e44cd63-68f0-41f2-b708-0eb3bf9f4a72", "HomeAddressLine1": "", "HomeAddressLine2": "",
            "HomeAddressCountryId": "", "HomeAddressState": "", "HomeAddressCity": "", "HomeAddressPostalCode": "",
            "HomeAddressContactNumber": "", "HasOtherResidenceship": False, "OtherResidenceshipPermitNumber": "",
            "EmployerName": "", "EmployerPhone": "", "EmployerAddress": "", "CurrentOccupationId": "",
            "PurposeOfJourneyId": "351336f9-b83c-4eab-9676-756f110b7a78",
            "MemberStateDestinationId": "5e44cd63-68f0-41f2-b708-0eb3bf9f4a72", "MemberStateSecondDestinationId": "",
            "MemberStateFirstEntryId": "", "NumberOfEntriesRequested": "", "IntendedStayDuration": "",
            "IsVisaIssuedBefore": False, "PreviousVisaNumber": "", "PreviousVisaIssuedCountryId": "",
            "PreviousFingerPrintStatus": "0", "FinalDestinationIssuedByCountryId": "", "BlsInvitingAuthority": "0",
            "InvitingAuthorityName": "", "InvitingCountryId": "", "InvitingCity": "", "InvitingZipCode": "",
            "InvitingAddress": "", "InvitingEmail": "", "InvitingContactNo": "", "InvitingFaxNo": "",
            "InvitingContactName": "", "InvitingContactSurname": "", "InvitingContactCountryId": "",
            "InvitingContactCity": "", "InvitingContactZipCode": "", "InvitingContactAddress": "",
            "InvitingContactContactNo": "", "InvitingContactEmail": "", "InvitingContactFaxNo": "",
            "CostCoveredById": "",
            "MeansOfSupportId": "", "OtherCitizenSurname": "", "OtherCitizenFirstName": "",
            "OtherCitizenNationalityId": "",
            "OtherCitizenDocumentNumber": "", "OtherCitizenFamilyRelationshipId": "",
            "ParentId": "56f41f5c-f58d-4989-9079-45436a89628e", "ApplicantId": "04b8dda3-2a7d-42f8-bb40-0f5ca04f52fc",
            "Id": "04b8dda3-2a7d-42f8-bb40-0f5ca04f52fc"
        }
    ]
    json_str = json.dumps(applicantsDetailsList)

    data = {
        'VisaType': '099a0161-b428-4a10-bb1e-639b7dee4fa0',
        'PreviousFingerPrintStatus_0': 0,
        'BlsInvitingAuthority_0': 0,
        'ScriptData': '',
        'Id': '56f41f5c-f58d-4989-9079-45436a89628e',
        'DataAction': 'Edit',
        'Email': '1248198090%40qq.com',
        'ApplicantsNo': '1',
        'LocationId': '8d780684-1524-4bda-b138-7c71a8591944',
        'Mobile': '18519203764',
        'MobileCountryCode': '%2B86',
        'AppointmentId': '56f41f5c-f58d-4989-9079-45436a89628e',
        'AppointmentFor': 'Individual',
        'ApplicantsDetailsList': quote(json_str),
        'TotalAmount': '',
        'ApplicantPhotoId': '3005f2e1-d2a4-48a8-a46b-c1a7d2f526c1',
        'ImageId': '',
        'AppointmentCategoryId': '5c2e8e01-796d-4347-95ae-0c95a9177b26',
        'AppointmentDate': quote('2024-04-05+00:00:00.000'),
        'SaveState': 'Application',
        'AppointmentNo': '',
        'CaptchaId': 'df7645c1-15a1-4306-affd-3e4c393307ef',
        'MinimumPassportValidityDays': '180',
        'ServerTravelDate': '',
        'RemoveChildren': 'False',
        'VisaTypeCode': 'SCHENGEN_VISA',
        '__RequestVerificationToken': 'CfDJ8ItFsDhqkWRIiWYGIDDs_SOny0gbm2bgiejMBJqDRNxcMM-kut2Elj-JIOmDcUW3aAXQCNW2I35XI0Hs0gBulQq6rXxBrkupeis9JWSEmWTXe7sT8Ioogw1OV1kVzUmRcXZCeNDz47BQgCGUIqi95H4gZw2azNRhjZ90LL0pI7Eo3hbga75sEcA3sZKB1O5TcQ',
        'X-Requested-With': 'XMLHttpRequest'
    }
    url = 'https://spain.blscn.cn/CHN/BLSAppointment/ManageAppointment'
    headers = utils.get_headers()
    res = requests.post(url, headers=headers, cookies=cookies, data=data)


def payment_request(cookies):
    data = {
        'Id': 'ee2c1641-630a-463a-83f8-b1a4f23906d6',
        'ValueAddedServices': '8b95ae10-9215-49d0-bff1-0b47a5e32b08_1',
        'LivenessDeclarationAccepted': False,
        'PaymentGatewayType': 4
    }
    url = 'https://spain.blscn.cn/CHN/payment/PaymentRequest'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
        'requestverificationtoken': 'CfDJ8ItFsDhqkWRIiWYGIDDs_SNgxmgfhD56DbAIkFZ2x-G_BLAWvaAWUiYm75m8DKc99fNhW62UTirqnjLTmnY8lDUqmx7f02mRIdVQ38X82gBf_5c6ZRnqE6l3cQfyfDLjet8gqWw1lEhI80wZKU9yeM-2l9iXHuV5EXK5CLm31d8ZYQAOy0V6sgX3j_olXLSJfg',
        'X-Requested-With': 'XMLHttpRequest'
    }
    res = requests.post(url, headers=headers, cookies=cookies, data=data)


if __name__ == '__main__':


    email_name = 'spain_appoint_1@163.com'
    # # email_pwd = 'spain_appoint_1$'
    # email_pwd = 'WZSDHGPXPWAQBQKT'
    # email_helper = EmailHelper()
    # email_helper.read_email(email_name, email_pwd)
    # user_name = '1248198090@qq.com'
    # password = '123$QWas'
    # user = User(user_name=user_name, password=password)
    # cookies = user.get_cookies()
    # print(cookies)
    # locationId = '8d780684-1524-4bda-b138-7c71a8591944'
    # categoryId = '5c2e8e01-796d-4347-95ae-0c95a9177b26'
    # visaType = '3033c6d3-579b-47e1-9602-91368d63025c'
    # visaSubType = 'd6f2b878-181f-4584-a595-babdbcac4298'
    # requestverificationtoken = ('CfDJ8ItFsDhqkWRIiWYGIDDs_SNgxmgfhD56DbAIkFZ2x'
    #                             '-G_BLAWvaAWUiYm75m8DKc99fNhW62UTirqnjLTmnY8lDUqmx7f02mRIdVQ38X82gBf_5c6ZRnqE6l3cQfyfDLjet8gqWw1lEhI80wZKU9yeM-2l9iXHuV5EXK5CLm31d8ZYQAOy0V6sgX3j_olXLSJfg')
    # target_date = '2024-03-28'
    # is_has = get_available_appointment_dates(cookies=cookies, target_date=target_date, locationId=locationId,
    #                                          categoryId=categoryId, visaType=visaType, visaSubType=visaSubType,
    #                                          token=requestverificationtoken)
    # if is_has:
    #     slots = get_available_slots_by_date(cookies=cookies, appointmentDate=target_date, locationId=locationId,
    #                                         categoryId=categoryId, visaType=visaType, visaSubType=visaSubType,
    #                                         token=requestverificationtoken)
    #     print(slots)

    # cookies = {
    #     '.AspNetCore.Antiforgery.BDmv0qjjHrI': 'CfDJ8ItFsDhqkWRIiWYGIDDs_SOE2ssT4pPcbQzMBx94rmMC5n52PiDLEKShcibzihHjSCbaTGA3V03tmBNaxGL6NlCUJeqlhTo7Sw48NfuyEtrcliCCBXIf3GPrVgwQatuC0zGfwyaAm2D_BVcvHWrkJK0',
    #     '.AspNetCore.Cookies': 'CfDJ8ItFsDhqkWRIiWYGIDDs_SPqKYinNfyKk9ZImFyFKUBds704IxTeQzYBn5ZtpY5XxMpLU6gSkjJrYsvAlrxWJul7S5LNcAsE0D7pmp4W5zHeSVZaaqvNyvIuoZCJKEC0mhFfYB3xOtUGl-2IbpHTxPX7VQvdXbVylFxn7VoY-YBbYIhwnd5xQPkFUBANJmVomE2q5OJnPAzyg7PKAAxopT8nJPHDe8w_TQTFpYk-4EhxWaQVjtWu8yFrQUJGx0TEh3eJ5hA2EEJqRmniJgSEr6IE9h6CQ5K0E06OcVSu_sTPeV3UOYNtOktUDaWXHb2XSY5C3Fc0-yfnuP1-zAc6Id8S7UWs2SA7zUk9H8eMLCk5tSGJaqxChQMgMxRyNyw95_-CT45fqWVwL9VO_6akdECh7JYc-oEKgncfqZxQVUngt_qOfZ8lMYg_v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vZpxSOvvhzYB0DdS9tQaXPKeOq-D75wHq4V_CxWzuKH8Fw8CO-1A4fGDDidh40TacxcoSRKvkuCvbFr3atsqsPpoY_O8AFmJIUBcDKs_e4F1IrM7f-96m-3cz9TH3RLPqyh5bP0cUn1PWCt5oO7qai6AXdx-10Q1DeSiCL4w4bnHWEJiwsNzhsws_-TPVaN2oXiEQCvGvHm_Fbt0_vKhwMdpP6jpqGOzDhfZ9t2PtyzOrrdqTtO9kaT6oCfh3_Gem8HhMfifjwaRTmBiG7UqjK2k5btpfoWCORgEsLLOWsBcUFuaMwgo13h6JIDqMzkCiAMWGH8eohm9jux0OaVw6JzTRupR_m-0buAEyyvBJimQ0FIEWVczYUGQgZwSl43k0XQD8RNz_oSCCpV8tcsRPtwxNabgowwiGPHY40bTRH-ncgwZQsjOLmPBrTo152fzOOpqe3exQ_iojV6CgvTuWFfxBsU0XHV-LLkfGUBg4o_t8pYt76Eq5liNIX3o3VLl_bZXghKWhTSee5GRa66kmCDWdkWCYQRGAMHylRly8ESHx4fWNny6sJXK-d7Xzn9zuyoawMxDRxTI20dXDLKcTJlXlDinGYZBwspb6BwbywcuVjKNkZY-SxgeFRfK6vsk_ZY0JN5IpGHT-VlYHWHE2y7oNezI-TDVEXkISW3kmdMkXRJWher_jdj-9PCW0UNx-Aa7RWe2MKz-gjd4C4Qd72fH5ecYmPvNjoYqindBdXvD7rwoDL-TI5AdNJRtrVBWFiTkm-FIDwLRxqxHQ9UXq6CZfTB44IwVNc-w-8Ug47i-QCV_--0lkUkWkVJyR6IfqFldK5C6-8fcF8uEfhSKVFuwk'
    # }
    # headers = {
    #     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
    #     'X-Requested-With': 'XMLHttpRequest'
    # }
    # url = 'https://spain.blscn.cn/CHN/blsAppointment/ManageAppointment?appointmentFor=Individual&applicantsNo=1&visaType=3033c6d3-579b-47e1-9602-91368d63025c&visaSubType=d6f2b878-181f-4584-a595-babdbcac4298&appointmentCategory=5c2e8e01-796d-4347-95ae-0c95a9177b26&location=8d780684-1524-4bda-b138-7c71a8591944&missionId=&data=gTzwyQKIHj9H8We%2fuSzlsymnNeC9LtZqf6zhSYuCZKQwkyPzAM%2fJaJUjEoBtofTCVjtN%2fX8INuAu4JgHx8CEQYm2OFGXDAyItjkeyLI7Z61kN81TYGzRlHq9NUyi3FuQUjAEjwmBFe3%2bOnif%2b9DobX2%2bsj21SQKLrbNxd7rcczA%3d'
    # res = requests.get(url, headers=headers, cookies=cookies)
    # print(res)
    # create_appointment(cookies)
